package com.pl.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pl.entity.Student;
import com.pl.entity.SysUser;
import com.pl.service.StudentService;
import com.pl.utils.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author 小胤
 * @Date 2024/4/25 20:04
 * @注释
 */
@RestController
@RequestMapping("/api/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 查询全部学生信息
     * @return
     */
    @RequestMapping("/finAllStudent")
    public R finAllStudent() {
        boolean result=studentService.finAllStudent();
        if (result){
            return R.ok();
        }
        return R.fail("查询全部失败");
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @RequestMapping("/list/page")
    public R queryByPage(@RequestParam Map<String, Object> map) {
        int pageNum = Integer.valueOf((String) map.get("pageNum"));
        int pageSize = Integer.valueOf((String) map.get("pageSize"));
        PageHelper.startPage(pageNum, pageSize);

        // 3.按照条件查询
        List<Student> list = studentService.queryByPage(map);

        // 4 返回R 内部有pageinfo
        if (list != null && list.size() > 0) { // 返回成功数据

            PageInfo<Student> pageInfo = new PageInfo<>(list);// 目的是的到总行数 总页数

            return R.ok(pageInfo);
        }
        return R.fail("查询数据为空");
    }

    /**
     * 批量删除学生信息
     * @param list
     * @return
     */
    @RequestMapping("/del/batch")
    public R deleteBatch(@RequestParam List<Integer>list){
        boolean result=studentService.deleteBatch(list);
        if (result){
            return R.ok();
        }
        return R.fail("删除失败");
    }

    /**
     * 新增学生信息，单条信息
     * @param student
     * @return
     */
    @RequestMapping("/save")
    public R addStudent(Student student) {
        boolean flag = studentService.addStudent(student);
        if (flag) {
            return R.ok();
        }
        return R.fail("新增数据失败");
    }

    /**
     * 更新数据
     * @param student
     * @return
     */
    @RequestMapping("/edit")
    public R updateStudentById(Student student) {
        boolean result = studentService.updateStudentById(student);
        if (result) {
            return R.ok();
        }
        return R.fail("更新失败");
    }

    /**
     * 通过id删除学生单行信息
     * @param id
     * @return
     */
    @RequestMapping("/del/{id}")
    public R removeStudentById(@PathVariable("id") int id) {
        boolean flag = studentService.removeStudentById(id);
        if (flag) {
            return R.ok();
        }
        return R.fail("删除失败");
    }



    @RequestMapping("/select")
    public Student finAllById(int id) {
        Student student = new Student();
        student.setId(id);
        student.setName("pl");
        return student;
    }

    @RequestMapping("/importStudentFile")
    public String importFile(@Param("file") MultipartFile file) {
        return studentService.importFile(file);
    }

    @RequestMapping("/exportStudentFile")
    public void exportFile(HttpServletResponse response) {
        studentService.exportFile(response);
    }
}
